const { IncomingForm } = require("formidable");
// console.log(process.cwd());
// __dirname : 获取执行文件的上层目录
// process.cwd : 可以获取执行命令的目录地址；
const form = new IncomingForm({
    // uploadDir: __dirname + "/../static/img",
    uploadDir:process.cwd()+"/static/img",
    keepExtensions: true
})

module.exports = function (req) {
    return new Promise((resolve,reject)=>{
        form.parse(req, (err, filds, file) => {
            if (err) {
                reject(err);
                return console.log(err);
            }
            // console.log(filds, file);
            let obj = {...filds,imgurl:file.img.newFilename};
            resolve(obj);
        })
    })
}